package com.foreknow.demo7;

import com.foreknow.demo5.D;

/**
 * 主人类
 */
public class Master {

    public void cure(Pet pet) { //pet = Dog对象
        //int a = (int) 34.5; 基本类型转换
        //Dog d = (Dog) pet; 如果将大类型(父类型)传递给小类型(子类型) --> 引用类型的强制类型转换(向下造型)
        if (pet.getHealth() < 60) {
            pet.toHospital();
        }
    }

//    public void cure(Penguin penguin) {
//        if (penguin.getHealth() < 50) {
//            penguin.setHealth(60);
//            System.out.println("治疗方式2......");
//        }
//    }

    public static void main(String[] args) {
        Master master = new Master();
        Pet pet = new Dog();  //父类的引用指向子类的对象
        pet.setHealth(20);
        master.cure(pet);

        pet = new Penguin();
        master.cure(pet);

        Dog dog = new Dog();
        dog.setHealth(20);
        master.cure(dog); //如果将小类型(子类型)传递给大类型(父类型) --> 引用类型的自动类型转换(向上造型)

        Penguin penguin = new Penguin();
        penguin.setHealth(15);
        master.cure(penguin);

//        Pet p = new Pet(); //抽象类是不能new的
//        master.cure(p); //报错

//        dog.setHealth(30);
//        master.cure(dog);

//        Penguin penguin = new Penguin();
//        penguin.setHealth(20);
//        master.cure(penguin);
    }

}
